<template>
    <div class="news">
      <div class="news-content" :class="titleType==2?'news-content2':''" ref="news">
        <div class="item-content"  v-show="child.state==1" v-for="(child,key) in list" :key="key">
          <img :src="child.cover" onerror="this.src='/imgs/noimg.jpg';this.onerror=null" alt="">
          <div class="details">
            <div class="title">{{ child.title }}</div>
            <div class="details-content">{{ getPlainText(child.content) }}</div>
            <div class="time">发布日期：{{ child.createTime }}</div>
            <el-button class="btn" :size="titleType==2?'':'mini'" type="primary" @click="jump(child)" round>查看详情</el-button>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    components: {
    },
    data(){
      return{
        dxalIndex:1,
        type:'',
        types:[],
        list:[],
        noimg:require('../../assets/noimg.jpg'),
        titleType:1
      }
    },
    methods:{
      //跳转页面
      jump(child){
        this.$router.push({ name: 'newsDetails', query: { id:child.id }});
      },
      //获取数据
      getData(){
        this.$apis.baseData.getPage({clazzCode:'XW',"pageNum": "1", "pageSize": "1000"}).then(res=>{
          this.list=res.data.list||[]
        })
      },
      //提取文字
      getPlainText(htmlContent) {
        // 创建一个新的DOM解析器
        var parser = new DOMParser();
        // 使用解析器解析HTML内容，并获取文档对象
        var doc = parser.parseFromString(htmlContent, 'text/html');
        // 创建一个文本节点的范围
        var range = doc.createRange();
        // 选择文档中的所有节点
        range.selectNodeContents(doc.body);
        // 使用范围提取文本内容
        return range.toString();
      },
      startWatchingWidth() {
        if(this.$refs.news.offsetWidth&&this.$refs.news.offsetWidth<=768){
          this.titleType=2
        }
    },
    },
    mounted(){
      this.getData()
      this.startWatchingWidth()
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .news{
    display: flex;
    flex-direction: column;
    background: #fff;
    align-items: center;
    padding-bottom: 30px;
  }
  .news-content{
    flex: 1;
    margin-top: 10px;
    width: 860px;
    max-width: 100%;
  }
  .item-content{
    display: flex;
    align-items: center;
    padding: 10px;
    margin-bottom: 16px;
    height: 200px;
    box-shadow: 2px 2px 5px -2px rgb(207, 207, 207);
    img{
      width: 260px;
      height: 180px;
    }
    .details{
      flex: 1;
      display: flex;
      flex-direction: column;
      position: relative;
      margin-left: 26px;
      height: 100%;
      .title{
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 16px;
        letter-spacing: 2px;
        width: 100%;
        overflow : hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .details-content{
        letter-spacing: 2px;
        width:100%;
        font-weight: bold;
        overflow : hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .time{
        margin-top: 10px;
        color: #666666;
      }
      .btn{
        position: absolute;
        bottom: 10px;
        left: 0;
      }
    }
    
  }
  .news-content2{
    .item-content{
      flex-direction: column;
      padding: 10px 20px;
      height: auto;
      .title{
        margin: 10px 0;
      }
      img{
        width: 100%;
        height: auto;
        max-height: 360px;
      }
      .details{
        height: auto;
        margin: 0;
        .btn{
          position: static;
          width: 100%;
          margin-top: 14px;
          height: 40px;
        }
      }
    }
  }
  </style>
  